package com.zjitc.controller;

import com.zjitc.entity.User;

import com.zjitc.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户控制器
 * 提供基本的用户管理功能
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    
    /**
     * 处理用户登录请求
     * @param user 包含用户名和密码的User对象
     * @return 登录结果，成功则返回JWT令牌
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody User user){
        Map<String, Object> response = new HashMap<>();
        String jwt = userService.login(user);
        
        if (jwt != null && !jwt.equals("登录失败")) {
            response.put("success", 200);
            response.put("token", jwt);
            response.put("message", "登录成功");
            
            // 设置响应头，同时支持两种方式
            return ResponseEntity.ok()
                .header("Authorization", "Bearer " + jwt)
                .header("Jwt", jwt)
                .body(response);
        } else {
            response.put("success",401);
            response.put("message", "登录失败，用户名或密码错误");
            return ResponseEntity.badRequest().body(response);
        }
    }
} 